<template>
	<view>
		<!-- 定位弹出提示 -->
		<u-popup :show="popupShow" mode="center" :round="18" :closeOnClickOverlay="false">
				<view class="popup">
					<view class="popup_top">
						<h3 v-if="popupType==1">请打开定位服务功能</h3>
						<h3 v-else-if="popupType==2">获取定位失败，是否授权打开定位</h3>
						<h3 v-else-if="popupType==3">请打开定位始终允许</h3>
						<h3 v-else-if="popupType==4">请打开通知管理</h3>
						<span v-if="popupType==1">设置>开启DPS</span>
						<span v-else-if="popupType==2 || popupType==3">权限>位置信息>始终允许</span>
						<span v-else-if="popupType==4">权限>通知管理>允许通知</span>
					</view>
					<view class="popup_conent">
						<!-- <view class="popup_list" @click="popupShow=false">取消</view> -->
						<view class="popup_list on" @click="popupTimeIMShow">去打开权限</view>
					</view>
				</view>
			</u-popup>
		<!-- 定位弹出提示 end-->
	</view>
</template>

<script setup>
	import { ref } from "vue"
	import {onLoad,onShow} from "@dcloudio/uni-app"
	
	//引入用户相关的小仓库
	import useUserStore from '@/store/user.js'
	const UserStore = useUserStore()

	const popupShow=ref(false);
	const popupType=ref(1);//1为gps没有打开 2.没有打开权限 3始终允许
	const longitudeTrue=ref(false);//判断是否打开定位
	const resultTrue=ref(true);//判断是否打开定位
	
	//导航
	// #ifdef APP-PLUS
	import {aMapLoc} from '@/composables/aMapLoc.js'
	const {enableBackgroundLocation,openSettingFun} = aMapLoc()
	import {aMapLocLon} from '@/composables/aMapLocLon.js'
	//#endif
	
	//导航地图初始化必须
	function NavSdkWXFun(){
		var sdkwx = uni.requireNativePlugin('AMap-NavSdkWX');
		//隐私合规接口
		sdkwx.updatePrivacyShow(true, true);
		sdkwx.updatePrivacyAgree(true);
		
		//检查定位权限, 注：Android在app首次启动的第一个页面onLoad里调用时，部分手机第一次可能不会弹出权限检查，可延迟调用检查定位权限
		sdkwx.checkLocationPermission(function(res){
			console.log(res,'权限判断')
			if (res.result == true) {
				//检查 始终允许 定位，注：1ios只有首次安装时才会弹框提醒和回调
				sdkwx.checkAlwaysLocationPermission(function(res2){
					console.log(res2,'始终允许')
					if(!res2.result){
						resultTrue.value=false;
						popupType.value=3; //
						popupShow.value=true;	
					}else{
						enableBackgroundLocation((cla)=>{
							if(!cla){
								popupType.value=4; //
								popupShow.value=true;	
							}else{
								// if(UserStore.startTrackTrue){
								// 	console.log('猎鹰开启成功!')
								// 	if(uni.getStorageSync('token')){
								// 		UserStore.startTrackFun();
								// 		aMapLocLon();
								// 		UserStore.startTrackTrue=false;
								// 	}
									
								// }
								
								
									if(uni.getStorageSync('token')){
										// UserStore.startTrackFun();
										aMapLocLon();
										// UserStore.startTrackTrue=false;
									}
							}
						});
					}
				});
			}else{
				//#ifdef APP
				resultTrue.value=false;
				//开启定位权限
					openSetting();
				// #endif 	
			}
		});
	}
	
	//点击开启定位权限
	function openSetting(){
		//判断是否打开dps功能
		 var context = plus.android.importClass("android.content.Context");
		 var locationManager = plus.android.importClass("android.location.LocationManager");
		 var main = plus.android.runtimeMainActivity();
		 var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
		 console.log(mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER))
		 //没有打开dps功能
		 if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
			 popupType.value=1; //
			 popupShow.value=true;
		 }else{
			console.log('GPS功能开启');
			uni.getLocation({
					type: 'wgs84',
					success: (res) => {
							console.log('当前位置的经度：' + res.longitude);
							console.log('当前位置的纬度：' + res.latitude);
					popupShow.value = false
					longitudeTrue.value = false
					},
				fail: error => {
					popupType.value=2; //
					popupShow.value=true;
				}
			});
		 }
	}
	
	//打开dps和定位权限
	function popupTimeIMShow(){
	 popupShow.value=false;
	 resultTrue.value=true;
	 //打开dps
	 if(popupType.value==1){
			var main = plus.android.runtimeMainActivity();
		 var Intent = plus.android.importClass('android.content.Intent');
		 var Settings = plus.android.importClass('android.provider.Settings');
		 var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
		 main.startActivity(intent); // 打开系统设置GPS服务页面
	 }else if(popupType.value==2 || popupType.value==3){ //打开定位权限
		 // 打开应用权限设置
		 var Intent = plus.android.importClass("android.content.Intent");
		 var Settings = plus.android.importClass("android.provider.Settings");
		 var Uri = plus.android.importClass("android.net.Uri");
		 var mainActivity = plus.android.runtimeMainActivity();
		 var intent = new Intent();
		 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
		 var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
		 intent.setData(uri);
		 mainActivity.startActivity(intent);
	 }else if(popupType.value==4){//打开通知管理
		 openSettingFun();
	 }
	}
	
	
	//显示打开
	function getLocationFun(){
		uni.getLocation({
		    type: 'wgs84',
		    success: function (res) {
						longitudeTrue.value=false;	
		    },
				fail: error => {
				 longitudeTrue.value=true;	
				 console.log(error)
				}
		});	
	}
	
	onShow(()=>{
		if(uni.getStorageSync('token') && resultTrue.value){
			//#ifdef APP
			//开启定位权限
				NavSdkWXFun();
			// #endif 	
		}
	})
	
	
</script>

<style lang="scss" scoped>
.popup{
		width: 600rpx;
		height: 226rpx;
		position: relative;
		overflow: hidden;
		border-radius: 36rpx;
		.popup_top{
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			height: 138rpx;
			h3{
				font-size: 32rpx;
				font-weight: 500;
				color: #262626;
				line-height: 46rpx;
			}
			span{
				font-size: 28rpx;
				font-weight: 500;
				color: #595959;
				line-height: 40rpx;
				margin-top: 8rpx;
			}
		}
		.popup_conent{
			position:absolute;
			bottom: 0;
			right: 0;
			left: 0;
			display:flex;
			height: 88rpx;
			border-top: 2rpx solid #f5f5f5;
			.popup_list{
				flex: 1;
				justify-content:center;
				align-items: center;
				display: flex;
				font-size: 32rpx;
				font-weight: 500;
				color: #8c8c8c;	
				transition: 0.3s all;
			}
			.popup_list:active{
				background: #eee;				
			}
			.popup_list.on{
				color: #5297ff;
			}
			.popup_list:first-child{
				border-right: 2rpx solid #f5f5f5;
			}
		}
	}
</style>
